<template>
  <div>
    <Card istitle>
      <template #title>商品添加</template>
      <template>
        <el-form
          ref="goodsDateForm"
          :model="goodsDate"
          label-width="80px"
          :rules="rules"
        >
          <el-form-item label="商品名称" prop="name">
            <el-input
              v-model="goodsDate.name"
              placeholder="商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="cate">
            <el-select v-model="goodsDate.cate" placeholder="请选择商品分类">
              <el-option
                v-for="(item, index) in cateArr"
                :key="index"
                :label="item.cateName"
                :value="item.cateName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品价格" prop="price">
            <el-input-number
              v-model="goodsDate.price"
              :min="1"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="商品图片" prop="imageUrl">
            <el-upload
              class="avatar-uploader"
              action="http://172.16.20.167:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img
                v-if="goodsDate.imageUrl"
                :src="
                  'http://172.16.20.167:5000/upload/imgs/goods_img/' +
                    goodsDate.imageUrl
                "
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品描述" prop="desc">
            <el-input type="textarea" v-model="goodsDate.desc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('goodsDateForm')"
              >添加商品</el-button
            >
          </el-form-item>
        </el-form>
      </template>
    </Card>
  </div>
</template>

<script>
import { addGoods, getGoodsCate } from '@/api/goods.js'
export default {
  data() {
    return {
      cateArr: [],
      goodsDate: {
        name: '',
        cate: '',
        price: 0,
        imageUrl: '',
        desc: ''
      },
      rules: {
        name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        cate: [
          { required: true, message: '请选择商品类型', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入商品价格', trigger: 'change' }
        ],
        imageUrl: [
          {
            required: true,
            message: '请选择图片',
            trigger: 'change'
          }
        ],
        desc: [{ required: true, message: '请输入商品描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 提交商品
    onSubmit(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 判断是否上传图片
          if (this.goodsDate.imageUrl) {
            // 上传数据
            const { code } = await addGoods({
              name: this.goodsDate.name,
              category: this.goodsDate.cate,
              price: this.goodsDate.price,
              imgUrl: this.goodsDate.imageUrl,
              goodsDesc: this.goodsDate.desc
            })
            if (code === 0) {
              setTimeout(() => {
                this.$router.push('/goods/list')
              }, 1000)
            }
          } else {
            this.$message.error('请选择图片！')
          }
        } else {
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.goodsDate.imageUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/gif']
      const isJPG = arr.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 获取分类
    async getCate() {
      const { categories } = await getGoodsCate()
      this.cateArr = categories
    }
  },
  created() {
    this.getCate()
  }
}
</script>

<style lang="less" scoped>
/deep/.el-form {
  width: 500px;
}
/deep/.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-right: 20px;
  }
  .el-upload:hover {
    border-color: #409eff;
  }
}
/deep/.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
/deep/.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
